"""
Case Type   : 资源池化
Case Name   : 资源池化场景下, 无任何业务压力, 进行probackup操作
Create At   : 2025-06-10
Owner       : jiangyan
Description :
    1.查看集群状态 cm_ctl query -Cv
    2.主机上初始化目录
    3.主机上添加备份实例
    4.主机上执行全量备份
    5.执行增量备份
    6.停止集群
    7.执行恢复
    8.启动集群
    9.环境恢复
Expect      :
    1.集群状态正常，数据库节点状态正常
    2.初始化目录成功
    3.添加备份实例成功
    4.执行全量备份成功
    5.执行增量备份成功
    6.停止集群成功
    7.执行恢复成功
    8.启动集群成功
    9.恢复环境成功

History     :
    1、2025-06-10 创建
    2、2025-07-12 改造用例为1p1s环境可执行
"""


import os
import unittest
import json
import time

from testcase.utils.ComThread import ComThread
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.Logger import Logger
from yat.test import macro
from yat.test import Node

Primary_SH = CommonSH('PrimaryDbUser')
Standby1_SH = CommonSH('Standby1DbUser')

@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_node = Node(node='PrimaryDbUser')
        self.constant = Constant()
        self.com = Common('PrimaryDbUser')
        self.enable_cbm_tracking = self.com.show_param("enable_cbm_tracking")

    def test_shared_storage(self):
        Primary_SH.exec_cmd_under_root(f'rm -rf {macro.DB_BACKUP_PATH}')
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:主机上初始化目录 expect:成功------'
        self.log.info(text)
        result = Primary_SH.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, f'enable_cbm_tracking=on')
        self.assertTrue(result)
        result = Primary_SH.exec_cm_ctl('stop')
        self.log.info(result)
        self.assertIn(
            self.constant.cm_stop_success_msg, result, "执行失败:" + text)
        result = Primary_SH.exec_cm_ctl('start')
        self.log.info(result)
        self.assertIn(
            self.constant.cm_start_success_msg, result, "执行失败:" + text)
        result = Primary_SH.exec_pro_backup_init(macro.DB_BACKUP_PATH)
        self.log.info(result)
        self.assertTrue(result, "执行失败:" + text)

        text = '-----step3:主机上添加备份实例 expect:成功------'
        self.log.info(text)
        result = Primary_SH.exec_pro_back_add(macro.DB_BACKUP_PATH, '0', "--enable-dss --instance-id 0 --vgname=\"+data,+log\"")
        self.log.info(result)
        self.assertTrue(result, "执行失败:" + text)

        text = '-----step4:主机上执行全量备份 expect:成功------'
        self.log.info(text)
        result = Primary_SH.exec_pro_backup_backup(macro.DB_BACKUP_PATH, '0', 'FULL', 'postgres')
        self.log.info(result)
        self.assertTrue(result, "执行失败:" + text)

        text = '-----step5:执行增量备份 expect:成功------'
        self.log.info(text)
        result = Primary_SH.exec_pro_backup_backup(macro.DB_BACKUP_PATH, '0', 'ptrack', 'postgres',
            f"--enable-dss --instance-id 0 --vgname=\"+data,+log\" -j 128", get_detail=True)
        self.log.info(result)
        self.assertIn('completed', result, "执行失败:" + text)
        result = Primary_SH.exec_probackup_show(macro.DB_BACKUP_PATH, '0')
        self.log.info(result)
        backupid_msg = result.splitlines()[-2]
        self.log.info(backupid_msg)
        backup_id = backupid_msg.split()[2]
        self.log.info("备份ID为:" + backup_id)

        text = '-----step6:停止集群 expect:成功------'
        self.log.info(text)
        result = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(result)
        self.assertTrue(result, "执行失败:" + text)

        text = '-----step7:执行恢复 expect 成功------'
        self.log.info(text)
        dss_start = ComThread(Primary_SH.exec_cmd_under_user,
                        args=('dssserver -M -D $DSS_HOME',))
        dss_start.setDaemon(True)
        dss_start.start()
        time.sleep(4)
        dss_start.join(10)
        self.log.info(dss_start.get_result())
        status = Primary_SH.exec_cmd_under_user("ps -ux | grep dssserver")
        self.log.info(status)
        result = Primary_SH.exec_pro_backup_restore(macro.DB_BACKUP_PATH, '0', backup_id, 
						restore_cmd= f' -b ptrack -d postgres -j 128 '
                        f'--incremental-mode=lsn', get_detail=True)
        self.log.info(result)
        self.assertIn('completed', result, "主机执行恢复失败" + text)
        result = Primary_SH.exec_cmd_under_user("dsscmd stopdss")
        self.log.info(result)

    def tearDown(self):
        text = '-----step8:启动集群 expect:成功------'
        self.log.info(text)
        result = Primary_SH.exec_cm_ctl('start')
        self.log.info(result)
        self.assertTrue(result, "执行失败:" + text)
        
        text = '-----step9:恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        result = Primary_SH.exec_cm_ctl('switchover', '-a')
        clear_cmd = f"rm -rf {macro.DB_BACKUP_PATH}"
        self.log.info(clear_cmd)
        clear_msg = Primary_SH.exec_cmd_under_root(clear_cmd)
        self.log.info(result)
        self.log.info(clear_msg)
        guc_res = Primary_SH.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_cbm_tracking={self.enable_cbm_tracking}')
        self.log.info(guc_res)
        self.assertTrue(guc_res)
        self.assertIn(self.constant.cm_switchover_success_msg,
                      result, '恢复环境失败')
        self.assertIn('', clear_msg, '删除目录失败')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")